package com.crr.controller;

import cn.hutool.core.bean.BeanUtil;
import com.crr.entities.Pay;
import com.crr.entities.PayDTO;
import com.crr.ret.ResultData;
import com.crr.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@Slf4j
@Tag(name = "支付微服务模块")
public class PayController {
    @Resource
    PayService payService;

    @PostMapping("/pay/add")
    @Operation(summary = "增加支付")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        System.out.println(pay);
        int res = payService.add(pay);
        if (res != 1) {
            throw new RuntimeException();
        }
        return ResultData.success("插入成功,返回值" + res);
    }

    @Operation(summary = "删除支付")
    @DeleteMapping("/pay/delete/{id}")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        int res = payService.delete(id);
        if (res != 1) {
            throw new RuntimeException();
        }
        return ResultData.success(res);
    }

    @PutMapping("/pay/update")
    @Operation(summary = "修改支付")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        //Pay实体只能service和dao层看见
        Pay pay = new Pay();
        BeanUtil.copyProperties(payDTO, pay);
        int res = payService.update(pay);
        if (res != 1) {
            throw new RuntimeException();
        }
        return ResultData.success("修改成功" + res);
    }

    @Operation(summary = "单个查询支付")
    @GetMapping("/pay/get/{id}")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        Pay pay = payService.query(id);
        if (null == pay) {
            throw new RuntimeException();
        }
        return ResultData.success(pay);
    }

    @Operation(summary = "查询全部")
    @GetMapping("/pay/getAll")
    public ResultData<List> getAll() {
        List<Pay> pays = payService.queryAll();
        if (null == pays) {
            throw new RuntimeException();
        }
        return ResultData.success(pays);
    }
    @Value("${server.port}")
    String port;

    @GetMapping("/pay/info")
    public String getInfoByConsul(@Value("${crr.username}") String name) {
        return name+"port:"+port;
    }
}
